<?php

$config   = ['path' => './'];
$excel    = new \Vtiful\Kernel\Excel($config);

// 读取测试文件
$excel->openFile('tutorial.xlsx')
    ->openSheet()->setType([
        \Vtiful\Kernel\Excel::TYPE_STRING,
        \Vtiful\Kernel\Excel::TYPE_STRING,
        \Vtiful\Kernel\Excel::TYPE_STRING,
        \Vtiful\Kernel\Excel::TYPE_STRING,
    ]);

var_dump($excel->nextRow()); // ['Item', 'Cost']

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 开始事务
    $conn->beginTransaction();
    // SQL 语句
    for ($i = 0; $i < 10505; $i++) {
        $data = $excel->nextRow();
        $sql = "INSERT INTO stations (provincialCode, number, name, hex) VALUES ('" . $data[0] . "','" . $data[1] . "','" . $data[2] . "','" . $data[3] . "')";
        $conn->exec($sql);
    }

    //  $conn->exec("INSERT INTO MyGuests (firstname, lastname, email) 
    //  VALUES ('Mary', 'Moe', 'mary@example.com')");
    //  $conn->exec("INSERT INTO MyGuests (firstname, lastname, email) 
    //  VALUES ('Julie', 'Dooley', 'julie@example.com')");

    // 提交事务
    $conn->commit();
    echo "新记录插入成功";
} catch (PDOException $e) {

    // 如果执行失败回滚
    $conn->rollback();
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
var_dump($excel->nextRow()); // NULL